package com.heima.behavior.advice;

import com.heima.behavior.dto.BehaviorDto;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @BelongsProject: heima-leadnews
 * @BelongsPackage: com.heima.behavior.advice
 * @Author: wangjian
 * @CreateTime: 2022-07-06  19:16
 * @Description: TODO
 * @Version: 1.0
 */
public class BehaviorAdvice {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Around("execution(* com.heima.behavior.service.impl.*.*(..))")
    public Object recordToRedis(ProceedingJoinPoint pjp) throws Throwable{
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Object[] args = pjp.getArgs();
        String methodName = signature.getMethod().getName();
        if(methodName.startsWith("save")){ //方法名都是以save开头的
            System.out.println(signature.getMethod().getParameters());
            BehaviorDto dto = (BehaviorDto)args[0];
            if(dto.getOperation()==0){
                redisTemplate.boundValueOps(methodName+"_"+dto.getUserId()+"_"+dto.getEquipmentId()+"_"+dto.getArticleId()).set("");
            }else{
                redisTemplate.delete(methodName+"_"+dto.getUserId()+"_"+dto.getEquipmentId()+"_"+dto.getArticleId());
            }
        }
        return pjp.proceed(args);
    }
}
